package com.emergency.embackend.utils;

import cn.hutool.core.date.StopWatch;
import com.emergency.embackend.service.DataViewService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author chddd
 * @description
 * @create 2024/3/27 22:37:23
 */
@Slf4j
@Component
@EnableScheduling
public class DataSchedule {
    @Autowired
    private DataViewService dataViewService;
    /**
     * 11,27,43,57,分钟执行一次
     */
    @Scheduled(cron = "45 11,27,43,57 * * * ? ")
    public void fillDataViews(){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("同步商品浏览量");
        try{
            log.info("开始同步商品浏览量数据...........");
            dataViewService.fillViews(RedisConstants.VIEW_VIDEO_COUNT_KEY,'0');
            dataViewService.fillViews(RedisConstants.VIEW_ARTICLE_COUNT_KEY,'1');
            dataViewService.fillViews(RedisConstants.VIEW_SHARE_COUNT_KEY,'2');
            dataViewService.fillViews(RedisConstants.VIEW_HELP_COUNT_KEY,'3');
        }catch (Exception e){
            log.error("同步商品数量出错:",e);
        }finally {
            stopWatch.stop();
            log.info("同步商品浏览量执行时间：{}", stopWatch.getTotalTimeSeconds());
        }
    }
}
